var insertIntoGUIDescriptor = function(array,object, idParent){

	if(object.element=="view"){
		array[array.length] = object;
	}else{
		//cerco il parent a cui devo appendere il nuovo elemento
		for(var i=0; i<array.length; i++){
					
			if(array[i].id==idParent){
				//ho trovato il parent e ci appendo il children
				array[i].children.push(object);
			}else if(array[i].children.length!=0){
				insertIntoGUIDescriptor(array[i].children,object, idParent);
			}
		}
	}
	
};

var addAttributeToElementInGUIDescriptor = function(array, key, value, idElement){
	
	for(var i=0; i<array.length; i++){		//cerco nel guidescriptor
		if(array[i].id==idElement){			//l'elemento in cui devo aggiungere il nuovo attributo
				array[i].attributes[key] = value;
		}else if(array[i].children.length!=0){
			addAttributeToElementInGUIDescriptor(array[i].children, key, value, idElement);
		}
	}
};

var addInnerHTMLToElementInGUIDescriptor = function(array, innerHTML, idElement){
	
	for(var i=0; i<array.length; i++){		//cerco nel guidescriptor
		if(array[i].id==idElement){			//l'elemento in cui devo aggiungere l'innerhtml
			array[i].innerHTML = innerHTML;			
		}else if(array[i].children.length!=0){
			addInnerHTMLToElementInGUIDescriptor(array[i].children, innerHTML, idElement);
		}
	}
};

var deleteElementFromGUIDescriptor = function(array, element){
	
	for(var i=0; i<array.length; i++){
		if(array[i].id==element.id){
			array.splice(i,1);
			return;
		}else if(array[i].children!=0){
			deleteElementFromGUIDescriptor(array[i].children, element);
		}
	}		
};

var getElement = function(array, idElement){

	for(var i=0; i<array.length; i++){
		if(array[i].id == idElement){
			return array[i];
		}else if(array[i].children != 0){
			var found = getElement(array[i].children, idElement);
			if(found!=null){
				return found;
			}
		}
	}
	
};

var getChildrenElement = function(array, idElement){
	
	for(var i=0; i<array.length; i++){
		if(array[i].id == idElement){
			return array[i].children;
		}else if(array[i].children != 0){
			var found =	getChildrenElement(array[i].children, idElement);
			if(found!=null){
				return found;
			}
		}
	
	}
};

var printGUIDescriptor = function(array, stringa){
	
	var str = "-" + stringa;
	
	for(var i=0; i<array.length; i++){
		console.log(str + array[i].id + " children: " + array[i].attributes.length);
		if(array[i].attributes.length!=0){
			for(var j=0; j<array[i].attributes.length; j++){
				console.log("*" + array[i].attributes[j].attribute);
			}
		}
		
		if(array[i].children.length != 0){
			printGUIDescriptor(array[i].children, str);			
		}
	}
};